package cn.zyzpp.controller.user;

import cn.zyzpp.entity.job.NewJob;
import cn.zyzpp.entity.response.Result;
import cn.zyzpp.entity.user.Admin;
import cn.zyzpp.entity.user.User;
import cn.zyzpp.service.job.JobService;
import cn.zyzpp.service.user.AdminService;
import cn.zyzpp.service.user.UserService;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Create by yster@foxmail.com 2018/5/28/028 13:09
 */
@Controller
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private AdminService adminService;
    @Autowired
    private HttpSession session;
    @Autowired
    private UserService userService;

    @Autowired
    private JobService jobService;

    //登录后台页面
    @RequestMapping("")
    public String admin() {
        if (session.getAttribute("or") != null && (Integer) session.getAttribute("or") == 1) {
            return "redirect:/admin/admin";
        }
        return "admin/adminLogin";
    }

    /**
     * 登录验证
     */
    @RequestMapping("/login")
    public String login(String name, String password) {
        Admin admin = adminService.queryByName(name);
        if (admin == null){
            //登录失败
            session.setAttribute("or", 0);
            String msg = "用户名输入错误";
            session.setAttribute("msg",msg);
            return "redirect:/admin";
        }
        if (new Md5Hash(password).toString().equals(admin.getPassword())) {
            //登录成功
            session.setAttribute("or", 1);
            return "redirect:/admin";
        } else {
            //登录失败
            session.setAttribute("or", 0);
            String msg = "密码输入错误";
            session.setAttribute("msg",msg);
            return "redirect:/admin";
        }
    }

    /**
     * 退出登录
     */
    @RequestMapping(value = "/logout")
    public String adminLogout() throws Exception {
        session.setAttribute("or",0);
        return "redirect:/admin";
    }

    /**
     * 后台管理页
     */
    @RequestMapping("/admin")
    public String adminDel() throws Exception {
        loginOr(session);
        return "admin/admin";
    }

    /**
     * 后台管理页
     */
    @RequestMapping("/admin/user")
    public String adminUser() throws Exception {
        loginOr(session);
        return "admin/adminUser";
    }

    /**
     * 后台管理页
     */
    @RequestMapping("/admin/job")
    public String adminJob()throws Exception {
        loginOr(session);
        return "admin/adminJob";
    }

    /**
     * 后台管理页
     */
    @RequestMapping("/admin/pass")
    public String adminPassPage()throws Exception {
        loginOr(session);
        return "admin/adminPassWord";
    }

    /**
     * 后台修改密码
     */
    @ResponseBody
    @RequestMapping("/pass/result")
    public String adminPass(String password)throws Exception {
        loginOr(session);
        try {
            adminService.updataPasswordByName("admin", password);
            return "修改成功";
        }catch (Exception e){
            return "修改失败";
        }
    }

    /**
     * 查询所有用户
     */
    @ResponseBody
    @RequestMapping("/queryAll")
    public Result<User> adminGetUser(int page,int limit) throws Exception {
        loginOr(session);
        Page<User> userPage = userService.queryAll(page-1,limit);
        List<User> userList = userPage.getContent();
        long count = userPage.getTotalElements();
        return new Result<>(0,"",count,userList);
    }

    /**
     * 更改用户状态
     */
    @ResponseBody
    @RequestMapping(value = "/userOrUser",method = RequestMethod.POST)
    public String updataUserOr(int id) throws Exception {
        loginOr(session);
        if(userService.updataOrUse(id) == 0){
            return "冻结成功";
        }else{
            return "解冻成功";
        }
    }

    /**
     * 查询所有发布
     */
    @ResponseBody
    @RequestMapping("/queryAllJob")
    public Result<NewJob> adminGetJobs(int page,int limit) throws Exception {
        loginOr(session);
        Page<NewJob> jobPage = jobService.queryByPage(page-1,limit);
        List<NewJob> jobList = jobPage.getContent();
        long count = jobPage.getTotalElements();
        return new Result<>(0,"",count, jobList);
    }

    /**
     * 关闭招聘
     */
    @ResponseBody
    @RequestMapping(value = "/jobOrUser",method = RequestMethod.POST)
    public String updataJobOrUse(int id) throws Exception {
        loginOr(session);
        if(jobService.updataOrUse(id)==0){
            return "关闭成功";
        }else{
            return "重新发布成功";
        }
    }


    private void loginOr(HttpSession session) throws Exception {
        if (session.getAttribute("or") == null) {
            throw new Exception("没有权限");
        }
        if (session.getAttribute("or") != null) {
            if (!(Integer.parseInt(session.getAttribute("or").toString()) == 1 ||
                    Integer.parseInt(session.getAttribute("or").toString()) == 0)) {
                throw new Exception("没有权限");
            }
        }
    }
}

